function loadTable() {
    let tid = $("#tid").val();
    $.ajax({
        type: "GET",
        url: "webapi/score/getAll",
        data: {
            tid: tid
        }
    }).done(function (map) {
        //console.log(map);
        let table = $("#table");
        table.empty();
        for (const key in map) {
            let num = $("<td>" + key.split("@")[0] + "</td>");
            let name = $("<td>" + key.split("@")[1] + "</td>");
            let attendanceScore = $("<td>" + "<input type='text' value='" + map[key].attendanceScore + "'>" + "</td>");
            let homeworkScore = $("<td>" + "<input type='text' value='" + map[key].homeworkScore + "'>" + "</td>");
            let button = $("<td>" +
                "<a class='button primary small' onclick='saveScore(this)'>保存</a>"
                + "</td>");

            let scoreInfo = $("<tr></tr>").append(num).append(name).append(attendanceScore).append(homeworkScore).append(button);
            table.append(scoreInfo);
        }
    })
}

function saveScore(scoreInfo) {
    let num_td = $(scoreInfo).parent().siblings("td").get(0);
    let numValue = $(num_td).text();

    let attendanceScore_td = $(scoreInfo).parent().siblings("td").get(2);
    let attendanceScoreValue = $(attendanceScore_td).children().val();

    let homeworkScore_td = $(scoreInfo).parent().siblings("td").get(3);
    let homeworkScoreValue = $(homeworkScore_td).children().val();

    $.ajax({
        type: "POST",
        url: "webapi/score/saveScore",
        data: {
            num: numValue,
            attendanceScore: attendanceScoreValue,
            homeworkScore: homeworkScoreValue
        }
    }).done(function (date){
        if (date === 1){
            alert("保存成功");
        } else {
            alert("保存失败");
            location.reload();
        }
    });
}